#include "MenuManager.h"
#include "MenuConfig.h"
#include "../Level/LevelManager.h"
#include <cocostudio/cocostudio.h>

using namespace cocostudio;
cocos2d::Scene* MenuManager::createScene()
{
    auto scene  = Scene::create();
    
    auto layer = MenuManager::create();
    
    scene->addChild(layer);
    return scene;
}

// on "init" you need to initialize your instance
bool MenuManager::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }
    
    setKeypadEnabled(true);
    
    m_screenSize = Director::getInstance()->getVisibleSize();
    initBackground();
    initalize();
    
    return true;
}
void MenuManager::initBackground()
{
    auto background = CSLoader::getInstance()->createNode("MenuBackground.csb");
    float scaleX = m_screenSize.width/background->getContentSize().width;
    float scaleY = m_screenSize.height/background->getContentSize().height;
    background->setScale(scaleX, scaleY);
    //  cocos2d::Vector<Node*> list = node->getChildren();
    background->setAnchorPoint(Vec2(0.5f,0.5f));
    background->setPosition(Vec2(m_screenSize.width / 2, m_screenSize.height / 2));
    this->addChild(background);
}
void MenuManager::initalize()
{
    auto node = CSLoader::getInstance()->createNode("MenuScene.csb");
    float scale = m_screenSize.height / node->getContentSize().height;
    
    node->setScale(scale);
  //  cocos2d::Vector<Node*> list = node->getChildren();
    node->setAnchorPoint(Vec2(0.5f,0.5f));
    node->setPosition(Vec2(m_screenSize.width / 2, m_screenSize.height / 2));
    
    btnSettings = static_cast<Button*>(node->getChildByTag(EMenuLayerTag::Buttons)->getChildByTag(EMenuLayerTag::btnSettings));
    pnlSettings = static_cast<Node*>(node->getChildByTag(EMenuLayerTag::Buttons)->getChildByTag(EMenuLayerTag::pnlSettings));
 
    cocos2d::ui::Button* btn = static_cast<Button*>(pnlSettings->getChildByTag(EMenuLayerTag::btnClose));
    
    btn->addClickEventListener(CC_CALLBACK_1(MenuManager::btnCloseClick, this));
    
    btnSettings->addClickEventListener(CC_CALLBACK_1(MenuManager::btnSettingClick, this));
    
    cocos2d::ui::Button* btnHard = static_cast<Button*>(node->getChildByTag(EMenuLayerTag::Buttons)->getChildByTag(EButtonMode::HardMode));
    cocos2d::ui::Button* btnNormal = static_cast<Button*>(node->getChildByTag(EMenuLayerTag::Buttons)->getChildByTag(EButtonMode::NormalMode));
    cocos2d::ui::Button* btnEasy = static_cast<Button*>(node->getChildByTag(EMenuLayerTag::Buttons)->getChildByTag(EButtonMode::EasyMode));
    
    btnHard->addClickEventListener(CC_CALLBACK_1(MenuManager::btnModeClick, this));
    btnNormal->addClickEventListener(CC_CALLBACK_1(MenuManager::btnModeClick, this));
    btnEasy->addClickEventListener(CC_CALLBACK_1(MenuManager::btnModeClick, this));
    
    
    
    this->addChild(node);

}
void MenuManager::btnModeClick(cocos2d::Ref *pSender)
{
    cocos2d::ui::Button* btn = static_cast<Button*>(pSender);
    switch(btn->getTag())
    {
        case EButtonMode::HardMode:
            Director::getInstance()->replaceScene(TransitionFade::create(1.0f,LevelManager::createScene(ELevelMode::Hard)));
            break;
        case EButtonMode::NormalMode:
            Director::getInstance()->replaceScene(TransitionFade::create(1.0f,LevelManager::createScene(ELevelMode::Normal)));
            break;
        case EButtonMode::EasyMode:
            Director::getInstance()->replaceScene(TransitionFade::create(1.0f,LevelManager::createScene(ELevelMode::Easy)));
            break;
    }
}

void MenuManager::btnCloseClick(cocos2d::Ref *pSender)
{
    pnlSettings->setVisible(false);
}
void MenuManager::btnSettingClick(cocos2d::Ref *pSender)
{
    Point p = pnlSettings->getPosition();
    pnlSettings->setOpacity(0);
    pnlSettings->setVisible(true);
    //pnlSettings->setScale(0.2f);
    pnlSettings->runAction(FadeIn::create(0.2f));
    pnlSettings->setPosition(pnlSettings->getPosition().x - 100,pnlSettings->getPosition().y);
    pnlSettings->runAction(MoveTo::create(0.2f, p));
    //pnlSettings->runAction(ScaleTo::create(0.2f,1.0f));
}
